package bb.lanxing.lib.devices.core.dfu;

import android.content.Context;
import android.content.Intent;
import bb.lanxing.lib.devices.core.ble.IGattClient;

public abstract class AbsDfuAdapter<CLIENT extends IGattClient> implements IDfuAdapter {
    protected CLIENT client;
    protected Context context;
    private int dfuModeState;

    public AbsDfuAdapter(Context context, CLIENT client) {
        this.context = context;
        this.client = client;
    }

    @Override
    public void release() {
        this.context = null;
        this.client = null;
    }

    public boolean isDfuOn() {
        return this.dfuModeState == 2;
    }

    public boolean isDfuOff() {
        return this.dfuModeState == 4;
    }

    public boolean isDfuTurningOn() {
        return this.dfuModeState == 1;
    }

    public boolean isDfuTurningOff() {
        return this.dfuModeState == 3;
    }

    public int getDfuModeState() {
        return this.dfuModeState;
    }

    public void setDfuModeState(int i) {
        this.dfuModeState = i;
        notifyDufStateChanged();
    }

    public void notifyDufStateChanged() {
        Intent intent = new Intent(IDfuAdapter.ACTION_DFU_MODE_STATE_CHANGED);
        intent.putExtra(IDfuAdapter.EXTRA_ADDRESS, this.client.getAddress());
        intent.putExtra(IDfuAdapter.EXTRA_DFU_MODE_STATE, this.dfuModeState);
        this.context.sendBroadcast(intent);
    }
}
